package com.kzd.gtgame.main.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.entity.WelfareCoupon
import com.kzd.gtgame.main.home.adapter.CouponPriceAdapter
import razerdp.basepopup.BasePopupWindow


class CouponButtonPopup(
    context: Context,
    val onWelfareCoupon: (view: View, position: Int, welfareCoupon: WelfareCoupon) -> Unit
) : BasePopupWindow(context) {

    init {
        popupGravity = Gravity.BOTTOM
        setOverlayNavigationBar(false)
    }

    private val mCouponPriceAdapter =
        CouponPriceAdapter(CouponPriceAdapter.Action.COUPON_PRICE_DRAW)

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_coupon_bottom)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.findViewById<ImageView>(R.id.ivCouponBottomClose).setOnClickAntiShakeListener {
            dismiss()
        }
    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 300L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 300L }
    }

    fun fillData(welfareCoupon: MutableList<WelfareCoupon>): CouponButtonPopup {
        contentView.findViewById<RecyclerView>(R.id.rvCouponBottomBody).apply {
            itemAnimator = null
        }.adapter = mCouponPriceAdapter.apply {
            setNewInstance(welfareCoupon)
        }
        mCouponPriceAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mCouponPriceAdapter.getItem(position)
            onWelfareCoupon(view, position, itemData)
        }
        return this
    }

    fun setData(index: Int, coupon: WelfareCoupon) {
        mCouponPriceAdapter.setData(index, coupon)
    }

}